package api

import (
	"context"
	"gitee.com/fangy1111/shop/userop"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"userop_bff/global"
)

// 留言列表
func MessageList(c *gin.Context) {
	userId, _ := c.Get("uid")
	list, err := global.MessageSrvConn.MessageList(context.Background(), &userop.MessageRequest{
		UserId: userId.(int32),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "查询留言列表失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": list,
	})
}

// 留言
func CreateMessage(c *gin.Context) {

	userId, _ := c.Get("uid")

	messageType := c.PostForm("message_type")
	messageTypeInt, _ := strconv.Atoi(messageType)

	subject := c.PostForm("subject")
	message := c.PostForm("message")
	file := c.PostForm("file")

	if len(messageType) == 0 || len(subject) == 0 || len(message) == 0 || len(file) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "缺少参数",
			"data": nil,
		})
		return
	}

	_, err := global.MessageSrvConn.CreateMessage(context.Background(), &userop.MessageRequest{
		UserId:      userId.(int32),
		MessageType: int32(messageTypeInt),
		Subject:     subject,
		Message:     message,
		File:        file,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "留言失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}
